class Favorite < ActiveRecord::Base
  
  # 一份最愛電報清單能有多份電報,但之間的關係不會重覆出現.
  has_and_belongs_to_many :computers, :uniq => true
  
  
  # 加入電報.
  def add_computer(computer)
    computers = self.computers;
    
    # 假如最愛電報內不含此電報才加入.
    find_computer = computers.exists?(computer)
    unless find_computer
      computers << computer
    end
  end
  
  # 清空所有電報.
  def clear_all
    self.computers.clear
  end
  
  # 移除電報.
  def remove_computer(computer)
    computers = self.computers;
    
    # 假如最愛電報內不含此電報才刪除.
    find_computer = computers.exists?(computer)
    if find_computer
      computers.delete(computer)
    end
  end
  
end
